package com.freesky.system.service.impl;

import com.freesky.common.config.ProjectConfig;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.common.utils.file.FileUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.mapper.TExportRecordMapper;
import com.freesky.system.service.ITExportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 导入文件记录Service业务层处理
 *
 * @author lwl
 * @date 2022-06-02
 */
@Service
public class TExportRecordServiceImpl implements ITExportRecordService {
    @Autowired
    private TExportRecordMapper tExportRecordMapper;

    /**
     * 查询导入文件记录
     *
     * @param id 导入文件记录ID
     * @return 导入文件记录
     */
    @Override
    public TExportRecord selectTExportRecordById(Long id)
    {
        return tExportRecordMapper.selectTExportRecordById(id);
    }

    /**
     * 查询导入文件记录列表
     *
     * @param tExportRecord 导入文件记录
     * @return 导入文件记录
     */
    @Override
    public List<TExportRecord> selectTExportRecordList(TExportRecord tExportRecord) {
        return tExportRecordMapper.selectTExportRecordList(tExportRecord);
    }

    @Override
    public TExportRecord selectRecordExportByStatus(TExportRecord tExportRecord) {
        return tExportRecordMapper.selectRecordExportByStatus(tExportRecord);
    }

    @Override
    public int insert(TExportRecord tExportRecord) {
        return tExportRecordMapper.insert(tExportRecord);
    }

    @Override
    public int update(TExportRecord tExportRecord) {
        return tExportRecordMapper.update(tExportRecord);
    }

    /**
     * 批量删除导入文件记录
     *
     * @param ids 需要删除的导入文件记录ID
     * @return 结果
     */
    @Override
    public int deleteTExportRecordByIds(Long[] ids)
    {
        return tExportRecordMapper.deleteTExportRecordByIds(ids);
    }

    /**
     * 删除导入文件记录信息
     *
     * @param id 导入文件记录ID
     * @return 结果
     */
    @Override
    public int deleteTExportRecordById(Long id)
    {
        return tExportRecordMapper.deleteTExportRecordById(id);
    }

    @Override
    public List<TExportRecord> selectRecordExportBizType(String exportBizType) {
        String userId = String.valueOf(SecurityUtils.getUserId());
        return tExportRecordMapper.selectRecordExportBizType(exportBizType,userId);
    }

    /**
     * 下载导入的文件
     *
     * @param id 导入文件记录ID
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult fileExcel(Long id, Long fileType, HttpServletResponse response, HttpServletRequest request) {
        try {
            //根据id获得当前记录信息
            TExportRecord tExportRecord = tExportRecordMapper.selectTExportRecordById(id);
            //判断文件名是否合法
            if (!FileUtils.checkAllowDownload(tExportRecord.getFileName())) {
                throw new Exception(StringUtils.format("文件名称({})非法，不允许下载。 ", tExportRecord.getFileName()));
            }
            String realFileName = tExportRecord.getFileName();
            String filePath = tExportRecord.getFileUrl();
            //下载源文件  fileType=1
            if ("1".toString().equals(fileType)) {
                realFileName = tExportRecord.getFileName();
                filePath = ProjectConfig.getDownloadPath() + tExportRecord.getFileName();
            }
            System.out.println("realFileName:"+realFileName+","+filePath);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, realFileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
            return AjaxResult.success("下载成功!");
        } catch (Exception e) {
            return AjaxResult.error("下载失败!");
        }
    }

}
